<template>
  <div class="ota-upload">
    <ft-table
      :listData="pageListData"
      v-bind="contentConfig"
      v-model:page="pageInfo"
    >
      <template #headerHandler>
        <el-button type="primary" size="medium" @click="handleNewData">
          {{ contentConfig.newBtnTitle ?? 'create new data' }}
        </el-button>
      </template>
      <template #update="scope">
        {{
          scope.row.updateTime === null
            ? 'no'
            : $filters.formatTime(scope.row.updateTime)
        }}
      </template>
      <template #handler="scope">
        <div class="handler">
          <el-button
            v-if="isUpdate"
            type="text"
            icon="el-icon-edit"
            size="mini"
            @click="handleEditClick(scope.row)"
          >
            edit
          </el-button>
          <el-button
            v-if="isDelete"
            type="text"
            icon="el-icon-delete"
            size="mini"
            @click="handleDeleteClick(scope.row)"
          >
            delete
          </el-button>
        </div>
      </template>
      <template #upload="scope">
        <div class="upload">
          <el-button
            type="primary"
            icon="el-icon-upload"
            size="mini"
            @click="otaUpload(scope.row)"
          >
            upload
          </el-button>
        </div>
      </template>
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </ft-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'
import { useStore } from '@/store'
import { IOtaUpload } from '@/store/main/hardware/types'

import FtTable from '@/base-ui/table'

export default defineComponent({
  components: {
    FtTable
  },
  props: {
    contentConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  emits: ['handleUploadClick', 'newBtnClick'],
  setup(props, { emit }) {
    // 1.请求页面数据
    const store = useStore()

    // 2.获取数据
    const getPageData = () => {
      store.dispatch('hardware/getPageListDataAction', {
        pageName: props.pageName
      })
    }
    getPageData()

    // 2.获取页面数据
    const pageListData = computed(() =>
      store.getters['hardware/pageListData'](props.pageName)
    )

    const otaUpload = (uploadInfo: IOtaUpload) => {
      console.log('uploadInfo', { ...uploadInfo })
      emit('handleUploadClick', uploadInfo)
    }

    // 4.剩余需要的插槽
    const otherPropSlots = computed(() => {
      return props.contentConfig.propList.filter((item: any) => {
        if (item.slotName === 'status') return false
        else if (item.slotName === 'create') return false
        else if (item.slotName === 'update') return false
        else if (item.slotName === 'handler') return false
        else if (item.slotName === 'upload') return false
        return true
      })
    })

    // 6.新建数据
    const handleNewData = () => {
      emit('newBtnClick')
    }

    return {
      pageListData,
      getPageData,
      otaUpload,
      otherPropSlots,
      handleNewData
    }
  }
})
</script>

<style scoped lang="less">
.ota-upload {
  border-top: 20px solid #f5f5f5;
  padding: 20px;
}
</style>
